require "import"
import "tools"
activity.getWindow().addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS).setStatusBarColor(0xff4DB6AC)
activity.setContentView("vTransform_")
format={"mp4","avi","flv","asf","gif","mkv","mov","mpeg","mpg","ra","rm","wmv"}
adp=ArrayAdapter(activity,android.R.layout.simple_spinner_item, String(format))
adp.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item)
transform.background=btBackground
sp.Adapter=adp
Define.onClick=function()
  Default.checked=false
  setEnable(true)
end
Default.onClick=function()
  Define.checked=false
  setEnable(false)
end
function setEnable(a)
  start.setEnabled(a)
  End.setEnabled(a)
  abitrate.setEnabled(a)
  frames.setEnabled(a)
  channel.setEnabled(a)
  volume.setEnabled(a)
  asample.setEnabled(a)
  aq.setEnabled(a)
end
Default.checked=true
setEnable(false)
oDir.text=LUA_SD.."/avHandle/output/"
iDir.onClick=function()
  ChoiceFile(LUA_SD,function(dir)
    iDir.text=dir
    vi.setVideoURI(Uri.parse(dir)).start().setMediaController(MediaController(this))
  end)
end
oDir.onClick=function()
  ChoicePath(LUA_SD,function(dir)
    oDir.text=dir.."/"
  end)
end
transform.onClick=function()
  if File(iDir.text).exists()
    if filename.text~=""
      local cmd="-i "..iDir.text.." -y "
      if Define.checked
        if start.text~=""
          cmd = cmd.."-ss "..start.text.." "
        end
        if End.text~=""
          cmd = cmd.."-t "..End.text.." "
        end
        if abitrate.text~=""
          cmd = cmd.."-ab "..abitrate.text.."k "
        end
        if channel.text~=""
          cmd = cmd.."-ac "..channel.text.." "
        end
        if asample.text~=""
          cmd = cmd.."-ar "..asample.text.." "
        end
        if volume.text~=""
          cmd = cmd.."-vol "..volume.text.." "
        end
        if frames.text~=""
          cmd = cmd.."-aframes "..frames.text.." "
        end
        if aq.text~=""
          cmd = cmd.."-aq "..aq.text.." "
        end
        if vs.text~=""
          cmd = cmd.."-s "..vs.text.." "
        end
        if vb.text~=""
          cmd = cmd.."-b "..vb.text.."k "
        end
        if vf.text~=""
          cmd = cmd.."-r "..vf.text.." "
        end
        if vframes.text~=""
          cmd = cmd.."-vframes "..vframes.text.." "
        end
        if aspect.text~=""
          cmd = cmd.."-aspect "..aspect.text.." "
        end
        if scale.text~=""
          cmd = cmd.."-vf scale=-1:iw*"..tostring(scale.text).." "
        end
      end
      cmd = cmd..oDir.text..filename.text.."."..sp.getSelectedItem()
      activity.newActivity("handle",{cmd})
     else
      print("请输入文件名")
    end
   else
    print("目标文件不存在")
  end
end
aspect.addTextChangedListener({
  afterTextChanged=function(a)
    if scale.text~=""
      scale.text=""
    end
    return false
end})
scale.addTextChangedListener({
  afterTextChanged=function(a)
    if aspect.text~=""
      aspect.text=""
    end
    return false
end})